package cn.hls.service.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.sun.tools.internal.ws.processor.generator.GeneratorUtil;

import cn.hls.dao.MemberDao;
import cn.hls.dao.SmsInfoDao;
import cn.hls.entity.Member;
import cn.hls.entity.SmsInfo;
import cn.hls.entity.vo.PageInfo;
import cn.hls.service.SmsInfoService;
import cn.hls.until.GenericsUtils;
@Service("smsInfoService")
public class SmsInfoSerivceBean extends BaseServiceBean<SmsInfo> implements
		SmsInfoService {

	@Resource(name="smsInfoDao")
	private SmsInfoDao dao;
	@Resource(name="memberDao")
	private MemberDao memberDao;
	@Override
	public boolean add(SmsInfo entity) throws Exception {
		if(entity.getMemberIds()!=null &&!entity.getMemberIds().isEmpty()){
			//根据编号集合获取发送短信信息
			List<Member>memberList=memberDao.loadMemberByIds(entity.getMemberIds());
			boolean flag=true;
			if(memberList!=null){
				for(Member item:memberList){
					if(StringUtils.hasText(item.getContractPhone())){//存在联系电话
						Integer status=GenericsUtils.sendMsg(new String[]{item.getContractPhone()}, entity.getContent());
						SmsInfo tempEntity=new SmsInfo();
						tempEntity.setSendTime(new Timestamp(new Date().getTime()));
						tempEntity.setMember(item);
						tempEntity.setStatus(status);
						tempEntity.setContent(entity.getContent());
						tempEntity.setOperator(entity.getOperator());
						flag&=super.add(tempEntity);
					}
				}
			}
			return flag;
		}else{
			return false;
		}
	}

	 

	@Override
	public PageInfo<SmsInfo> loadSmsInfo(PageInfo<SmsInfo> page, SmsInfo entity)
			throws Exception {
		return dao.loadSmsInfo(page, entity);
	}
}
